
*** 1 a. Prices 2016-2019

*** Import all csv files in NOK/MWh

forval y = 2016/2019 {
import delimited "data\in\elspot-prices_`y'_hourly_nok.csv", varnames(1) clear 
destring oslo krsand bergen molde trheim tromsø, replace dpcomma
tempfile p`y'
save `p`y'', replace 
}
clear 
forval y = 2016/2019 {
    append using `p`y''
}
g DMYhm = v1 + substr(hours,1,2) + "00"
g double eventtime = clock(DMYhm, "DMYhm")
format eventtime %tc 
gen dato = date(v1, "DMY") 
format dato %td
order dato, first
keep dato hours oslo eventtime

g day = day(dato)
g month = month(dato)
g year = year(dato)

sort dato hours 
*Convert units from NOK/MWh to NOK/kWh
replace oslo=oslo/1000 
g price = oslo + 0.049 + 0.24375 + 0.0125 + 0.2016 //regular schedule 
// strømpris + marginaltapsledd + nettleie + energifondet Enova + forbruksavgift til staten

keep if inlist(month,1,2,3,4,12)
drop if (year == 2016 & month < 12) | (year == 2019 & month > 4)
tab year month 
g time = eventtime 
format %16.0g time
g hour = substr(hours,1,2)
destring hour, replace 

replace price = price * 0.10048 

forval y = 2017/2019 {
su time if year == `y' - 1 & month == 12 & day == 1 & hour == 0
loc dec = r(mean)
su time if year == `y' & month == 1 & day == 1 & hour == 0
loc jan = r(mean)
su time if year == `y' & month == 2 & day == 1 & hour == 0
loc feb = r(mean)
su time if year == `y' & month == 3 & day == 1 & hour == 0
loc mar = r(mean)
su time if year == `y' & month == 4 & day == 1 & hour == 0
loc apr = r(mean)
su time if year == `y' & month == 4 & day == 30 & hour == 0
loc may = r(max) + 1 

su price if (year == `y' - 1 & month == 12) | (year == `y' & month <= 4)
loc avg = round(r(mean), .001)
loc sd = round(r(sd), .001)
su time if year == `y' & month == 3 & day == 10 & hour == 0
loc x = r(mean)

line price eventtime if (year == `y' - 1 & month == 12) | (year == `y' & month <= 4), c(L L) lc(black red) lw(vthin..) xtitle("") xlab(`dec' "Dec" `jan' "Jan" `feb' "Feb" `mar' "Mar" `apr' "Apr" `may' "May")  ytitle("") ylab(0(0.05)0.3) title(`=`y'-1'-`y') /// 
text(0.25 `x' "mean = `avg'" "sd = `sd'", place(se) box bcolor(white) just(left))  ///
///ytitle("USD/" "kwh", orientation(horizontal) placement(12)) ///
legend(off) name(fig1_`y', replace)
}

/*
collapse (mean) price, by(month day hours)
replace month = 0 if month == 12 
sort month day hours
line price month, c(L L) lc(black red) lp(shortdash solid) lw(thin..) xtitle("") ytitle("USD/" "kwh", orientation(horizontal) placement(12)) legend(off) name(fig1a_prices, replace)
*/



*** 1 b. Prices 2019-2020

use data\in\spotpris_sept2019_mai2020, clear 

g day = day(dato)
g month = month(dato)
g year = year(dato)
keep if inlist(month,12,1,2,3,4)

label var hour "tid på døgnet"
label var hour "time of day"
g byte treatday = 0
  replace treatday = 1 if year == 2019 & month == 12 & day == 10
  replace treatday = 2 if year == 2019 & month == 12 & day == 19
  replace treatday = 3 if year == 2020 & month == 1 & day == 23
  replace treatday = 4 if year == 2020 & month == 1 & day == 30
  replace treatday = 5 if year == 2020 & month == 2 & day == 13
  replace treatday = 6 if year == 2020 & month == 2 & day == 26
  replace treatday = 7 if year == 2020 & month == 3 & day == 5
  replace treatday = 8 if year == 2020 & month == 3 & day == 31
  replace treatday = 9 if year == 2020 & month == 4 & day == 28

g byte Peak = inrange(hour,16,21)
g byte NonPeak = 1 - Peak

g Control = Pris + 0.049 + 0.24375 + 0.0125 + 0.2016 //regular schedule 
// strømpris + marginaltapsledd + nettleie + energifondet Enova + forbruksavgift til staten

g Treatment = Pris + 0.049 + 0.05 + 0.0125 + 0.2016 if treatday == 0 | Peak == 0 // non-event days and non-peak
  replace Treatment = Pris + 0.049 + 10 + 0.0125 + 0.2016 if treatday != 0 & Peak == 1 // event days

* USD = 9.5377 NOK 
replace Control = Control * 0.10046
replace Treatment = Treatment * 0.10046 
g time = Time 
format %16.0g time

su time if year == 2019 & month == 12 & day == 1 & hour == 0
loc dec = r(mean)
su time if year == 2020 & month == 1 & day == 1 & hour == 0
loc jan = r(mean)
su time if year == 2020 & month == 2 & day == 1 & hour == 0
loc feb = r(mean)
su time if year == 2020 & month == 3 & day == 1 & hour == 0
loc mar = r(mean)
su time if year == 2020 & month == 4 & day == 1 & hour == 0
loc apr = r(mean)
su time if year == 2020 & month == 4 & day == 30 & hour == 0
loc may = r(max) + 1 

su Control if year == 2020 | (year == 2019 & month == 12)
loc avg = round(r(mean), .001)
loc sd = round(r(sd), .001)
su Treatment if year == 2020 | (year == 2019 & month == 12)
loc tavg = round(r(mean), .001)
loc tsd = round(r(sd), .001)
su time if year == 2020 & month == 3 & day == 10 & hour == 0
loc x = r(mean)

line Control Treatment Time if year == 2020 | (year == 2019 & month == 12), c(L L) lc(black red) lp(solid solid) lw(vthin..) ylab(0(0.1)1.1) xtitle("") xlab(`dec' "Dec" `jan' "Jan" `feb' "Feb" `mar' "Mar" `apr' "Apr" `may' "May") title(2019-2020) legend(off) /// 
text(0.9 `x' "C mean = `avg'" "C sd = `sd'" "T mean = `tavg'" "T sd = `tsd'" , place(se) box bcolor(white) just(left)) ///
///ytitle("USD/" "kwh", orientation(horizontal) placement(12)) legend(ring(0) pos(3) bmargin(zero)) ///
name(fig1_2020, replace)



gr combine fig1_2017 fig1_2018 fig1_2019 fig1_2020, iscale(*1.2) col(2) l1title("USD/" "kwh", orientation(horizontal) placement(12)) b1title() name(fig1_prices, replace)

graph export "output\fig1_prices.pdf", as(pdf) name("fig1_prices") replace
